<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.min.js"
        type="application/javascript"></script>
    <link rel="stylesheet" href="https://layui.11dz.cn/layui-v2.6.8/layui/css/layui.css">
    <script src="https://layui.11dz.cn/layui-v2.6.8/layui/layui.js"></script>
</head>

<body>
    <div id="app">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;"><legend>二楼</legend></fieldset>
        <table class="layui-table">
            <tr>
                <td>类别</td>
                <td>所用寝室</td>
                <td>进入时间</td>
                <td>操作</td>
            </tr>
            <tr v-for="(item,index) in info">
                <td>{{item['name']}}</td>
                <td>{{item['dorm']}}</td>
                <td>{{item['inTime']}}</td>
                <td>
                    <button v-if="item['dorm']==myDorm" @click="back(item.Id)" class="layui-btn layui-btn-sm layui-btn-normal">已归</button>
                    <button v-if="item['dorm']=='空'" @click="use(item.Id)" class="layui-btn layui-btn-sm layui-btn-normal">使用</button>
                </td>
            </tr>
        </table>
        <button type="button" class="layui-btn" @click="refresh">刷新</button>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                myDorm: localStorage.getItem("myDorm"),
                info: "",
            },
            methods: {
                back(id) {
                    if (!localStorage.getItem("myDorm")) { this.bindDorm(); return; }
                    fetch(`http://39.107.221.29/t2/backToilet.php?id=${id}&dorm=${this.enc(this.myDorm)}`, { method: "GET" }).then((res) => {
                        return res.json()
                    }).then((res) => {
                        layer.msg(res.code ? "成功" : "失败");
                        this.refresh();
                    });
                },
                use(id) {
                    if (!localStorage.getItem("myDorm")) { this.bindDorm(); return; }
                    fetch(`http://39.107.221.29/t2/useToilet.php?id=${id}&dorm=${this.enc(this.myDorm)}`, { method: "GET" }).then((res) => {
                        return res.json()
                    }).then((res) => {
                        layer.msg(res.code ? "成功" : "失败");
                        this.refresh();
                    });
                },
                refresh() {
                    fetch("http://39.107.221.29/t2/getToilet.php", { method: "GET" }).then((res) => { return res.json() }).then((res) => {
                        this.info = res.sort((a, b) => { return a.Id - b.Id })
                        layer.msg("加载完成");
                    });
                },
                enc(str) {
                    var encoded = "";
                    for (i = 0; i < str.length; i++) {
                        var a = str.charCodeAt(i);
                        var b = a ^ 123;
                        encoded = encoded + String.fromCharCode(b);
                    }
                    return encoded;
                },
                bindDorm() {
                    layer.open({
                        title: '绑定'
                        , content: `<div class="layui-form-item">
<label class="layui-form-label">寝室号</label>
<div class="layui-input-block">
  <input type="text" name="title" required  lay-verify="required" placeholder="请输入寝室号" autocomplete="off" class="layui-input" id="myDorm">
  <button class="layui-btn" lay-submit lay-filter="formDemo" onclick="localStorage.setItem('myDorm',document.getElementById('myDorm').value);location.reload()">保存</button>
</div>
</div>`
                    });
                }
            },
            created() {
                this.refresh()
                String.prototype.enc = function () {
                    let bytes = [];
                    for (var i = 0; i < this.length; i++) {
                        bytes.push(this.charCodeAt(i * 2 + 3));
                    }
                    return bytes.join(',');
                }
            },
        });
    </script>
</body>

</html>